using UnityEngine;
using System.Collections;

public class ElectrifiedFloorControl : StateBehaviour<ElectrifiedFloorControl>
{
    #region Attributes

    public GameObject particleEffectHitPlayer;

    public float damage = 0.25f;
    public float timeFirstTime = 0.0f;
    public float timeIsOn = 1.0f;
    public float timeIsOff = 1.0f;
    public bool isBeginsOn = false;
    public float timeSinceHit = 0.0f;
    public float timeOffset = 1.0f;

    private bool isFirstTime;
    private float timeToChangeState;
    [HideInInspector]
    public GameObject effect;
    #endregion

    #region Methods

    public void SetTimeToChangeState(float timeOffset)
    {
        if (this.isFirstTime)
        {
            this.timeToChangeState += this.timeFirstTime;
            this.isFirstTime = false;
        }
        else
            this.timeToChangeState += timeOffset;
    }

    public bool IsTimeToChangeState()
    {
        this.timeToChangeState -= Time.deltaTime;
        return 0.0f >= this.timeToChangeState;
    }

    public void DamagePlayer(Collider other)
    {
        if (other.tag == TagConstants.TAG_PLAYER && this.State == ElectrifiedFloorOnState.Instance && this.timeSinceHit + this.timeOffset < Time.timeSinceLevelLoad)
        {
            timeSinceHit = Time.timeSinceLevelLoad;
            GameControl.FindPlayer().ChangeEnergy(this.damage, true);

            GameObject.Instantiate(this.particleEffectHitPlayer, other.ClosestPointOnBounds(transform.position), Quaternion.identity);
        }
    }

    #endregion

    #region Unity Methods

    void Start () 
    {
        this.effect = this.transform.Find(TextConstants.LIGHTNING_NAME).gameObject;
        if (this.timeFirstTime > 0.0f)
            this.isFirstTime = true;
        else
            this.isFirstTime = false;

        if (this.isBeginsOn)
            this.State = ElectrifiedFloorOnState.Instance;
        else
            this.State = ElectrifiedFloorOffState.Instance;

        this.updateCalledIn = StateBehaviourMethodsName.Update;
	}

    void OnTriggerEnter(Collider other)
    {
        DamagePlayer(other);
    }

    void OnTriggerStay(Collider other)
    {
        DamagePlayer(other);
    }

    #endregion

    #region StateBehaviour

    public override void OnUpdate()
    {
        
    }

    public override ElectrifiedFloorControl GetBehaviuor()
    {
        return this;
    }

    #endregion
}
